<template>
	<view class="login">
		<!-- 登录页 -->
		<text>*</text>用户名：
		<input type="text" v-model="username">
		<text>*</text>密码：
		<input type="password" v-model="password">
		
		<button 
		type="primary" 
		:disabled="username!=''&&password!=''? false:true"
		@click="goUserinfo"
		>登 录</button>
		
		<button 
		plain 
		type="primary"
		@click="goRegister"
		>注 册</button>
		
		<view class="tip">
			<text @click="setNet">没网络？</text>
			<text @click="goIndex">算了，我还是不登录了</text>
		</view>
	</view>
</template>

<script>
	import { http } from "../../utils/request.js"
	export default {
		data() {
			return {
				username:"",
				password:""
			};
		},
		methods:{
			// 跳转到个人中心（用户名和密码都正确）
			goUserinfo(){
				http({
					url:"/prod-api/api/login",
					method:"POST",
					data:{
						username:this.username,
						password:this.password
					}
				}).then(res=>{
					if(res.data.code!= 200){
						uni.showToast({
							title:res.data.msg,
							icon:'none'
						})
					}else{
						//用户名和密码都正确，记录token,和登录的用户名并跳转到个人中心页面
						uni.setStorageSync("token", res.data.token)
						uni.setStorageSync("username", this.username)
						uni.switchTab({
							url:"/pages/userInfo/userInfo"
						})
					}
				})
			},
			// 跳转到注册页
			goRegister(){
				uni.navigateTo({
					url:"/pages/register/register"
				})
			},
			// 网络设置
			setNet(){
				uni.navigateTo({
					url:"/pages/guidePage/guidePage"
				})
			},
			// 跳转到主页面
			goIndex(){
				uni.switchTab({
					url:"/pages/index/index"
				})
			}
		}
	}
</script>

<style lang="scss">
.login{
	padding: 40rpx;
	text{
		color: red;
	}
	input{
		margin: 20rpx 0;
		background-color: #f8f8f8;
		border-radius: 20rpx;
		padding: 10rpx 20rpx;
	}
	button{
		margin-top: 40rpx;
		border-radius: 20rpx;
	}
	button::after{
		border: 0;
	}
	.tip{
		margin-top: 20rpx;
		font-size: 26rpx;
		display: flex;
		justify-content: space-between;
	}
}
</style>
